#include "../include/passenger.h"
#include "../include/menu.h"
#include "../include/trips.h"
#include "../include/longin.h"

//创建头节点
PASS *passengerHead(PASS *head)
{
    head = (PASS *)malloc(sizeof(PASS));
    if(head == NULL)
    {
        perror("malloc head");
        exit(1);//申请空间失败
    }
    memset(head, 0, sizeof(PASS));

    return head;
}

//创建新用户
PASS *newPassenger(PASS *head)
{
    assert(head);
    PASS *node = NULL;
    PASS *end = head;

    char strname[20] = {0};
    //输入用户信息
    printf("输入用户名：");
    scanf("%s", strname);

    while(end->next != NULL)
    {
        if(!strcmp(end->next->pas.name, strname))
        {
            printf("用户已存在！\n");
            printf("请前往登录！\n");
            logMessage(LOG_WARNING, "NewPass already exists!");

            break;
        }
        end = end->next;
    }

    if(end->next == NULL)
    {
        //创建新节点
        node = (PASS *)malloc(sizeof(PASS));
        if(node == NULL)
        {
            perror("malloc pas node");
            exit(1);
        }
        memset(node, 0, sizeof(PASS));

        strcpy(node->pas.name,strname);

        char pdstr[20] = {0};
        printf("密码只能由数字和字母组成！\n");
        printf("输入密码：");
        while(1)
        {
            scanf("%s", pdstr);
            int len = strlen(pdstr);
            int i = 0;
            while(pdstr[i] != '\0')
            {
                if(!isalnum(pdstr[i]))
                {
                    break;
                }
                i++;
            }

            if(i == len)
            {
                strcpy(node->pas.password,pdstr);

                break;
            }
            else
            {
                printf("密码输入格式有误！\n");
                printf("请重新输入！\n");
            }
        }

        char phon[20] = {0};
        printf("电话类型为 +86\n");
        printf("请输入电话号码：");

        while(1)
        {
            int i = 0;
            scanf("%s", phon);
            int len = strlen(phon);
            while(phon[i] != '\0')
            {
                if(!isalnum(phon[i]))
                {
                    break;
                }
                i++;
            }
           
            if(len == 11 && i == len)
            {
                strcpy(node->pas.tele, phon);
                
                break;
            }
            else
            {
                printf("电话号码格式有误！\n");
                printf("请重新输入：");
            }
        }

        //将新节点链接到已有的链表中
        end->next = node;
        node->next = NULL;
        printf("注册成功！\n");

        logMessage(LOG_INFO, "addNewPass %s -- %s", node->pas.name, node->pas.password);
    }

    return head;
}

//乘客登录
PASS *passengerLogin(PASS *head)
{
    assert(head);
    PASS *end = head->next;
    char str[100] = {0};
    int i = 2;
    printf("请输入用户名：");
    scanf("%s", str);
    while(end != NULL)
    {
        if(!strcmp(end->pas.name, str))
        { 
            while(i >= 0)
            {
                printf("请输入密码：");
                scanf("%s", str);
                if(!strcmp(end->pas.password, str))
                {
                    printf("登录成功！\n");
                    logMessage(LOG_INFO, "passLogin %s -- %s", end->pas.name, end->pas.password);

                    return end;
                }
                else if(i > 0)
                {
                    printf("输入错误！\n还有%d次机会\n", i);
                    i--;
                }
                else  
                {
                    i--;
                }  
            }
        }
        end = end->next;
    }
    if(i == 2)
    {
        printf("用户不存在！\n请注册！\n");
        logMessage(LOG_ERROR, " No pass!");

        return NULL;
    }
    if(end == NULL)
    {
        printf("登录失败！\n");
        logMessage(LOG_ERROR, "passLogin error!");

        return NULL;
    }
}

//乘客查看个人信息
void output(PASS *head)
{
    assert(head);
    PASS *end = head;
   
    printf("%s----%s----%s\n", "用户名", "密码", "电话");
    printf("%s--%s--%s\n", end->pas.name, end->pas.password, end->pas.tele);
    printf("--%s--\t%s\t  %s----%s  \n", "车次", "发车时间", "起点", "终点");


    printf("--%s--      [%d-%d-%d %d:%d]    %s----%s\n",end->pas.passhead.trip, 
    end->pas.passhead.time.tm_year, end->pas.passhead.time.tm_mon + 1, 
    end->pas.passhead.time.tm_mday, end->pas.passhead.time.tm_hour,
    end->pas.passhead.time.tm_min, end->pas.passhead.start, end->pas.passhead.destination);

}

//乘客改密码
PASS *passengerPwd(PASS *head)
{
    assert(head);
    PASS *end = head->next;
    char passwd[20] = {0};
    char username[20] = {0};

    printf("请输入你的用户名：");
    scanf("%s", username);

    while (end != NULL)
    {
        if (!strcmp(end->pas.name, username))
        {
            printf("请输入你的原密码：");
            scanf("%s", passwd);
            if (!strcmp(passwd, end->pas.password))
            {
                printf("请输入你要改的密码：");
                scanf("%s", passwd);
                char tel[20] = {0};
                printf("请输入电话号：");
                scanf("%s", tel);
                if(strcmp(tel, end->pas.tele))
                {
                    printf("电话号不存在！\n");
                    logMessage(LOG_ERROR, "No tele!");

                    break;
                }

                char ch[7] = {0};
                //生成验证码
                chrand(ch);
                char str[7] = {0};
	            printf("请输入验证码：");
	            scanf("%s",str);
    
                //处理字符串
                chcmp(str);
                chcmp(ch);
                if(!strcmp(str, ch))
                {
                    strcpy(end->pas.password, passwd);
                    printf("密码更改成功！\n");
                    logMessage(LOG_INFO, "passChangepwd %s -- %s", end->pas.name, end->pas.password);

                    break;   
                }
                else
                {
                    printf("验证码输入有误！\n");
                    printf("修改密码失败！\n");
                    logMessage(LOG_ERROR, "passChangepwd error!");

                    break;
                }
            }
            else
            {
                printf("输入密码有误!\n");
                logMessage(LOG_ERROR, "passChangepwd error!");

                break;
            }
        }
        else 
        {
            printf("用户不存在！\n");
            logMessage(LOG_ERROR, "No pass!");

            break;
        }
        end = end->next;
    }

    return head;
}

//释放链表
int freepas(PASS *head)
{
    PASS *end = NULL;
    end = head;
    if(end == NULL)
    {
        printf("link is empty!\n");
    }
    else
    {
        while(head != NULL)
        {
            head = head->next;
            free(end);
            end = head;
        }
    }
    return 0;
}

//生成验证码
char* chrand(char* ch)
{
	srand(time(NULL));

	int i = 0;
	int a, b, c, d;

	while(i < 6)
	{
		a = 48 + rand() % 10;
		b = 65 + rand() % 26;
		c = 97 + rand() % 26;
		d = 1 +  rand() % 10;

		if(d <= 3)
		{
			ch[i] = a;
		}
		else if(d <= 6)
		{
			ch[i] = b;
		}
		else
		{
			ch[i] = c;
		}

		i++;
	}
	ch[6] = '\0';
	printf("验证码：");
	printf("%s\n", ch);

	return ch;
}

//验证码改大小写
void chcmp(char* str)
{
	while(*str != '\0')
	{
		if(*str >= 'a' && *str <= 'z')
		{
			*str = *str - 32;
		}
		*str++;
	}
}